package com.woniuxy.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.woniuxy.dto.RecordDTO;
import com.woniuxy.entity.Record;
import com.woniuxy.mapper.RecordMapper;
import com.woniuxy.service.SignRecordService;
import com.woniuxy.util.ResponseEnum;
import com.woniuxy.util.ResponseUtil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;

/**
 * 签约记录服务实现类
 * 实现签约记录相关的业务逻辑
 */
@Service("signRecordService")
public class SignRecordServiceImpl implements SignRecordService {
    @Resource
    private RecordMapper recordMapper;

    /**
     * 查询所有签约记录
     * @return 返回所有签约记录的响应结果
     */
    @Override
    public ResponseUtil queryAll(Integer pageNum, Integer pageSize) {
        // 开启分页
        PageHelper.startPage(pageNum, pageSize);
        List<RecordDTO> list = recordMapper.queryAll();
        // 使用 PageInfo 包装分页结果，它会包含了正确的总记录数等信息
        PageInfo<RecordDTO> pageInfo = new PageInfo<>(list);
        // 构建一个符合前端期望的 map
        HashMap<String, Object> resultData = new HashMap<>();
        // 从 PageInfo 对象中获取真实的总记录数
        resultData.put("total", pageInfo.getTotal());
        // 获取当前页的数据列表
        resultData.put("list", pageInfo.getList());
        return ResponseUtil.get(ResponseEnum.RECORD_QUERY_OK, resultData);
    }

    /**
     * 根据居民ID查询签约记录
     * @param residentId 居民ID
     * @return 返回指定居民的签约记录
     */
    @Override
    public ResponseUtil queryByResidentId(Integer residentId) {
        List<RecordDTO> recordList = recordMapper.queryByResidentId(residentId);
        return ResponseUtil.get(ResponseEnum.RECORD_QUERY_OK,recordList);
    }

    /**
     * 根据居民ID查询所有签约记录（包括所有状态）
     * @param residentId 居民ID
     * @return 返回指定居民的所有签约记录
     */
    @Override
    public ResponseUtil queryAllByResidentId(Integer residentId) {
        List<RecordDTO> recordList = recordMapper.queryAllByResidentId(residentId);
        return ResponseUtil.get(ResponseEnum.RECORD_QUERY_OK,recordList);
    }

    /**
     * 添加新的签约记录
     * @param record 签约记录对象
     * @return 返回添加操作的响应结果
     */
    @Override
    public ResponseUtil addRecord(Record record) {
        // 初始状态为0（待支付）
        record.setStatus(0);
        // 根据历史签约记录设置签约类型
        Integer history = recordMapper.countByResidentAndOrg(record.getResidentId(), record.getOrgId());
        if (history != null && history > 0) {
            record.setRecordType("非首次签订");
        } else {
            record.setRecordType("首次签约");
        }
        List<RecordDTO> recordDTOS = recordMapper.queryAllByResidentId(record.getResidentId());
        for (RecordDTO recordDTO : recordDTOS) {
            if (recordDTO.getStatus() == 0){
                return ResponseUtil.get(ResponseEnum.SIGN_WAIT_PAY);
            }else if(recordDTO.getStatus() == 1 && recordDTO.getPakId() == record.getPakId()){
                return ResponseUtil.get(ResponseEnum.SIGN_USER_EXIST);
            }
        }
        Integer rows = recordMapper.addRecord(record);
        if (rows != null && rows > 0) {
            // 返回新创建的recordId
            return ResponseUtil.get(ResponseEnum.RECORD_ADD_OK, record.getRecordId());
        } else {
            return ResponseUtil.get(ResponseEnum.FAIL);
        }
    }

    /**
     * 删除指定ID的签约记录
     * @param id 记录ID
     * @return 返回删除操作的响应结果
     */
    @Override
    public ResponseUtil deleteRecord(Integer id) {
        Integer flag = recordMapper.deleteRecord(id);
        return flag > 0 ? ResponseUtil.get(ResponseEnum.RECORD_DELETE_OK) : ResponseUtil.get(ResponseEnum.FAIL);
    }

    /**
     * 更新签约记录
     * @param record 签约记录对象
     * @return 返回更新操作的响应结果
     */
    @Override
    public ResponseUtil updateRecord(Record record) {
        Integer flag = recordMapper.updateRecord(record);
        if (flag > 0) {
            return ResponseUtil.get(ResponseEnum.RECORD_UPDATE_OK);
        } else {
            return ResponseUtil.get(ResponseEnum.FAIL);
        }
    }

    @Override
    public ResponseUtil queryRecordByStatus(Integer status) {
        List<RecordDTO> recordDTOS = recordMapper.queryRecordByStatus(status);
        return ResponseUtil.get(ResponseEnum.RECORD_QUERY_OK,recordDTOS);
    }


}
